var Report = require('../models/Report');
var eventproxy = require('eventproxy');

exports.queryAll = function (req,res){
	var query = {}
	var event_id = req.query.event_id?req.query.event_id:'';
	var skip = req.query.skip?parseInt(req.query.skip):1;
	var skip_convert = skip*60*60/30;

	if(event_id){
		query.event_id = event_id;
	}

	Report.count(query,function(err,count){
		var times = Math.floor(count/skip_convert);
		console.log(times)

		var ep = new eventproxy();
		ep.after('reports',times,function(reports){
			res.json(reports)
		})

		for(var i = 0;i<times;i++){
			Report.findOne(query)
					.skip(i*skip_convert)
					.limit(1)
					.sort({'create_date':-1})
					.exec(function(err,report){
						ep.emit('reports',report)
			})
		}

	})



	
}

exports.report = function(req,res){
	res.render('report')
}